import React, {PureComponent, Fragment} from "react";
import moment from "moment";
import {Row, Col, Form,  Card, Input, Icon, Button, Divider, message } from "antd";
import DescriptionList from '../../../components/DescriptionList';
import Upload from "../../../components/Upload";
import Authorized from '../../../utils/Authorized';
import styles from '../../Pbulic.less';

const FormItem = Form.Item;
const { TextArea } = Input;
const { Description } = DescriptionList;


@Form.create()
export default class Acceptance extends PureComponent {
  constructor(props) {
    super(props);
    this.state={
      fileList: [],
    }
  }


  componentDidMount() {

  }


  handleFormSubmit(handleResult,e) {
    e.preventDefault();
    const {dispatch, inspectId, form} = this.props;
    form.validateFieldsAndScroll((err, values) => {
      if (!err) {
        dispatch({
          type: `inspection/fetch_inspection_examine`,
          payload: {
            ...values,
            inspectId,
            handleResult,
          }
        }).then(() => {
          this.handleReset();
          message.success("稽查审核已处理");
        }).then(() => {
          dispatch({
            type: "inspection/fetch_inspection_handle",
            payload: {
              inspectId,
            }
          })
        });
      }
    });
  }


  handleSubmitStatus(type,e) {
    e.preventDefault();
    const {dispatch, inspectId, form} = this.props;
    dispatch({
      type: `inspection/fetch_inspection_status_${type}`,
      payload: {
        inspectId,
      }
    }).then(() => {
      message.success(`${type==="examine" ? '审核' : type==='acceptance' ? '验收' : ''}正在处理中`);
    }).then(() => {
      dispatch({
        type: "inspection/fetch_inspection_handle",
        payload: {
          inspectId,
        }
      })
    });
  }

  handleReset = () => {
    this.setState({
      fileList: [],
    })
    this.props.form.resetFields();
  };



  render() {

    const {data, status, isRole, title, inspectProject, inspectStatus, lastStatus, loadingsubmit , baseUrl} = this.props;
    const {fileList} = this.state;
    // status 01待办  02处理中 03审核中 04结束
    const { getFieldDecorator } = this.props.form;


    return (
      <Fragment>
        {/* 05结束（审核结果） -- 对应状态 05结束 */}
        {
          status === "05" &&
          <Fragment>
            <DescriptionList size="large" title={`审核结果`} style={{ marginBottom: 32 }}>
              <Description term="审核时间">{data.createTime && moment(data.createTime).format("YYYY-MM-DD HH:mm")}</Description>
              <Description term="审核结果">{data.handleResult=="1" ? '通过' : '不通过'}</Description>
              <Description term="描述">{data.processDesc}</Description>
            </DescriptionList>
          </Fragment>
        }


        {/* 04审核中 -- 对应状态 04审核中 */}
        {
          status === "04"  && inspectStatus==="04" && lastStatus==="04"  &&
          <Fragment>
            <Authorized authority={'10255'} noMatch={
              // 验收中
              <DescriptionList size="large" title={"审核中"} col={2}  style={{marginBottom: 32}}>
              </DescriptionList>
            }>
            <DescriptionList size="large" title={"审核"} style={{ marginBottom: 32 }}>
              <Form layout="horizontal" style={{marginTop:20}}>
                <Row gutter={24} className={styles.info_form}>
                  <Col span={24}>
                    <FormItem colon={false}>
                      {getFieldDecorator("processDesc", {
                        rules: [{max: 200, message: "不超过200个字"}]
                      })(
                        <TextArea
                          placeholder="请输入描述"
                          autosize={{minRows: 3, maxRows: 6}}
                        />
                      )}
                    </FormItem>

                  </Col>
                </Row>
                <Row gutter={24}>
                  <Col span={24} style={{textAlign: 'center', padding: 30}}>
                    <Button
                      onClick={this.handleFormSubmit.bind(this,"1")}
                      type="primary"
                      size="large"
                      style={{marginRight: 15}}
                      loading={loadingsubmit}
                      // loading={loading.model_submit}
                    >
                      审核通过
                    </Button>
                    <Button
                      onClick={this.handleFormSubmit.bind(this,"0")}
                      type="default"
                      size="large"
                      loading={loadingsubmit}
                    >
                      审核不通过
                    </Button>
                  </Col>
                </Row>
              </Form>
            </DescriptionList>
            </Authorized>
          </Fragment>
        }


        {/* 待审核  */}
        {
          status === "03" && inspectStatus==="03" && lastStatus==="03" &&
          <Fragment>
            <DescriptionList size="large" title={"待审核"} col={2}  style={{marginBottom: 32}}>
              {
                <Authorized authority={'10255'} >
                  <Row gutter={24}  style={{marginTop: 32,marginBottom: 32,textAlign:'center'}}>
                    <Col span={24}>
                      <Button
                        onClick={this.handleSubmitStatus.bind(this,'examine')}
                        type="primary"
                        size="large"
                      >
                        审核
                      </Button>
                    </Col>
                  </Row>
                </Authorized>
              }
            </DescriptionList>
          </Fragment>
        }


        {/* 整改中 -- 对应状态 02整改中 */}
        {
          status === "02" &&
          <Fragment>
            <DescriptionList size="large" title={"整改"} style={{ marginBottom: 32 }}>
              <Description term="整改时间">{data.rectifyTime && moment(data.rectifyTime).format("YYYY-MM-DD HH:mm")}</Description>
              <Description term="整改描述">{data.handleResult}</Description>
            </DescriptionList>
            <DescriptionList size="large" >
              <Description >
                {
                  data.processPhoto && data.processPhoto.length>0 &&
                  <div >
                    <Upload
                      listType="picture-card"
                      fileList={data.processPhoto.map(item=>{
                        item.url = baseUrl+item.url;
                        return item;
                      })}
                      disabled={true}

                      // onPreview={this.handlePreview}
                    />
                  </div>
                }
              </Description>
            </DescriptionList>
          </Fragment>
        }



        {/* 稽查 -- 对应状态 01待办 */}
        {
          status === "01" &&
          <Fragment>
            <DescriptionList size="large" title={"稽查"} style={{ marginBottom: 32 }}>
              <Description term="稽查时间">{data.inspectTime && moment(data.inspectTime).format("YYYY-MM-DD HH:mm")}</Description>
              <Description term="稽查人">{data.creatorName}</Description>
              <Description term="区域">{data.districtName}</Description>
              <Description term="点位">{data.pointName}</Description>
              <Description term="项目">{data.inspectProject && inspectProject[parseInt(data.inspectProject)-1]}</Description>
              <Description term="问题描述">{data.problemDesc}</Description>
            </DescriptionList>
            <DescriptionList size="large" >
              <Description className={styles.upload_only_show} >
                {
                  data.inspectPhoto && data.inspectPhoto.length > 0 &&
                  <Upload
                    listType="picture-card"
                    fileList={data.inspectPhoto.map(item=>{
                      item.url = baseUrl+item.url;
                      return item;
                    })}
                    disabled={true}
                    // onPreview={this.handlePreview}
                  />
                }
              </Description>
            </DescriptionList>
            <Divider style={{ marginBottom: 32 }} />
            <DescriptionList size="large" title={title} style={{ marginBottom: 32 }}>
              <Description term="维保单位">{data.maintenanceName}</Description>
              <Description term="整改人">{data.rectifyManName}</Description>
              <Description term="处理时限">{data.handleTime} 小时</Description>
            </DescriptionList>
          </Fragment>
        }

      </Fragment>
    );
  }
}
